package cn.edu.suda.schoolBus.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.Appointment;
import cn.edu.suda.schoolBus.bean.AppointmentExample;
import cn.edu.suda.schoolBus.dao.AppointmentMapper;
import cn.edu.suda.schoolBus.service.AppointmentService;

@Service
public class AppointmentServiceImpl implements AppointmentService {

	@Autowired
	private AppointmentMapper appointmentDao;

	private Appointment appointment;

	private AppointmentExample appointmentExample;

	@Override
	public List<Appointment> getAppointmentInfo(int pId, int aStatus, int dcnStatus) {
		return appointmentDao.selectAppointmentInfo(pId, aStatus, dcnStatus);
	}

	@Override
	public int addAppointment(Appointment appointment) {
		return appointmentDao.insertSelective(appointment);
	}

	@Override
	public int cancelAppointment(int aId) {
		appointment = new Appointment();
		appointment.setaId(aId);
		appointment.setaStatus(0);
		return appointmentDao.updateByPrimaryKeySelective(appointment);
	}

	@Override
	public Appointment getAppointment(int pId, int dcnId) {
		appointmentExample = new AppointmentExample();
		appointmentExample.createCriteria().andRelateToPassengerEqualTo(pId).andRelateToDynamicCarNumberEqualTo(dcnId)
				.andAStatusEqualTo(1);
		List<Appointment> appointments = appointmentDao.selectByExample(appointmentExample);
		appointment = null;
		if (appointments.size() == 1) {
			appointment = appointments.get(0);
		}
		return appointment;
	}

	@Override
	public Appointment getAppointment(int aId) {
		return appointmentDao.selectByPrimaryKey(aId);
	}

}
